{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ProjectQ Demo\n",
    "## Compiling code for IBM QE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import the IBM setup, the gates, and the compiler engine:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import projectq.setups.ibm # Imports the default compiler to map to IBM QE\n",
    "from projectq.backends import IBMBackend\n",
    "from projectq.ops import All, Entangle, Measure\n",
    "from projectq import MainEngine"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create the compiler using the default compiler engines for the IBM backend and allocate a quantum register of 3 qubits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "engine = MainEngine(IBMBackend(use_hardware=True, num_runs=1024, verbose=True, device='ibmqx4'),\n",
    "                    engine_list=projectq.setups.ibm.get_engine_list())\n",
    "qureg = engine.allocate_qureg(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "Entangle the quantum register:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Entangle | qureg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Measure the quantum register and run the circuit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- Authenticating...\n",
      "IBM QE user (e-mail) > dsteiger@phys.ethz.ch\n",
      "IBM QE password > ········\n",
      "- Running code: \n",
      "include \"qelib1.inc\";\n",
      "qreg q[3];\n",
      "creg c[3];\n",
      "h q[2];\n",
      "cx q[2], q[0];\n",
      "h q[1];\n",
      "cx q[1], q[0];\n",
      "h q[0];\n",
      "measure q[0] -> c[0];\n",
      "h q[2];\n",
      "measure q[2] -> c[2];\n",
      "h q[1];\n",
      "measure q[1] -> c[1];\n",
      "- Waiting for results...\n",
      "- Done.\n",
      "11100 with p = 0.388671875*\n",
      "01100 with p = 0.0478515625\n",
      "10000 with p = 0.0029296875\n",
      "00000 with p = 0.4482421875\n",
      "01000 with p = 0.0166015625\n",
      "11000 with p = 0.025390625\n",
      "00100 with p = 0.0263671875\n",
      "10100 with p = 0.0439453125\n"
     ]
    }
   ],
   "source": [
    "All(Measure) | qureg\n",
    "engine.flush()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Output the measurement result:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "print([int(q) for q in qureg])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Inspecting the gates required for a 5-qubit entangle on IBM QE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a new compiler with a command printer as a back-end:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from projectq.backends import CommandPrinter\n",
    "engine2 = MainEngine(CommandPrinter(),\n",
    "                     engine_list=projectq.setups.ibm.get_engine_list())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Allocate a quantum register of 5 qubits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "qureg2 = engine2.allocate_qureg(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Entangle quantum register (and run the circuit):\n",
    "\n",
    "A 5-qubit entangle operation requires one H gate and 4 CNOTs\n",
    "if the qubit chip would support CNOTs between arbitrary qubits:\n",
    "```\n",
    "H | Qubit[0]\n",
    "CX | ( Qubit[0], Qubit[1] )\n",
    "CX | ( Qubit[0], Qubit[2] )\n",
    "CX | ( Qubit[0], Qubit[3] )\n",
    "CX | ( Qubit[0], Qubit[4] )\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Allocate | Qureg[0]\n",
      "Allocate | Qureg[1]\n",
      "H | Qureg[1]\n",
      "CX | ( Qureg[1], Qureg[0] )\n",
      "Allocate | Qureg[2]\n",
      "H | Qureg[2]\n",
      "CX | ( Qureg[2], Qureg[0] )\n",
      "Allocate | Qureg[3]\n",
      "H | Qureg[3]\n",
      "CX | ( Qureg[3], Qureg[0] )\n",
      "Allocate | Qureg[4]\n",
      "H | Qureg[4]\n",
      "CX | ( Qureg[4], Qureg[0] )\n",
      "H | Qureg[0]\n",
      "H | Qureg[1]\n",
      "H | Qureg[2]\n",
      "H | Qureg[3]\n",
      "H | Qureg[4]\n"
     ]
    }
   ],
   "source": [
    "Entangle | qureg2\n",
    "engine2.flush()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As IBM's QE chip does not support CNOTs between arbitrary qubits, the compiler automatically\n",
    "changes the abstract entangle circuit to an equivalent circuit which takes\n",
    "into account the hardware constraints of the IBM QE chip. The resulting circuit is shown above as the output of the command printer."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simulating a circuit for the IBM QE chip"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a new compiler with our high-performance quantum simulator as a back-end:\n",
    "\n",
    ">### Note \n",
    ">Our Simulator can simulate much larger circuits but here we imported the compiler for the IBM QE chip:\n",
    "```\n",
    "import projectq.setups.ibm\n",
    "```\n",
    "> and therefore the compiler will only allow circuits which can be mapped to the IBM QE chip. Running larger simulations is easily possible by using the default compiler (see other example codes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from projectq.backends import Simulator\n",
    "engine3 = MainEngine(Simulator())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Allocate a 5-qubit quantum register and apply an entangle operations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "qureg3 = engine3.allocate_qureg(5)\n",
    "Entangle | qureg3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Measure the quantum register and run the circuit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "All(Measure) | qureg3\n",
    "engine3.flush()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Output the measurement result:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 0, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "print([int(q) for q in qureg3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
